layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;
    
    laydate.render({
    	elem:'#areaNumber'
    });

    /**
     * 动盘明细表管理
     */
    var MoveDiskDetailed = {
        tableId: "moveDiskDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    MoveDiskDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'inventory', sort: true, title: 'Type', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'Product Code', style:'font-weight: 600;'},
            {field: 'goodsName', sort: true, title: 'Product Name', style:'font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: 'Item No', style:'font-weight: 600;'},
            {field: 'warehouseName', sort: true, title: 'Warehouse', style:'font-weight: 600;'},
            {field: 'areaName', sort: true, title: 'Area', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: 'Location', style:'font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: 'Tray No', style:'font-weight: 600;'},
            {field: 'num', sort: true, title: 'Original Stock', style:'font-weight: 600;'},
            {field: 'trueNum', sort: true, title: 'Corrected Amount', style:'font-weight: 600;'},
            {field: 'goodsbatchs', sort: true, title: 'Batch', style:'font-weight: 600;'},
            /*{field: 'goodsbatchs', sort: true, title: 'Original Production Date', style:'font-weight: 600;'},
            {field: 'newgoodsbatchs', sort: true, title: 'Corrected Production Date', style:'font-weight: 600;'},*/
            {field: 'createTime', sort: true, title: 'Operation Time'},
            {field: 'createUsername', sort: true, title: 'Operator'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    MoveDiskDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['areaNumber'] = $("#areaNumber").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['areaId'] = $("#areaId").val();
        queryData['positionId'] = $("#positionId").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        table.reload(MoveDiskDetailed.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    MoveDiskDetailed.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/moveDiskDetailed/add';
    };

    /**
     * 导出excel按钮
     */
    MoveDiskDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(MoveDiskDetailed.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    MoveDiskDetailed.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/moveDiskDetailed/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    MoveDiskDetailed.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/moveDiskDetailed/delete", function (data) {
                Feng.success("Deletion successful!");
                table.reload(MoveDiskDetailed.tableId);
            }, function (data) {
                Feng.error("Deletion failed! " + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + MoveDiskDetailed.tableId,
        url: Feng.ctxPath + '/moveDiskDetailed/list',
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: MoveDiskDetailed.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        MoveDiskDetailed.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        MoveDiskDetailed.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        MoveDiskDetailed.exportExcel();
    });

    // 单击行事件
    table.on('row(' + MoveDiskDetailed.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + MoveDiskDetailed.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + MoveDiskDetailed.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
    //查询库区
    form.on('select(warehouseId)',function(){
    	$("#areaId").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/basicArea/Querylist',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"areaWarehouseId":$("#warehouseId").val()},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>选择库区</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.id+"'>"+value.areaName+"</option>";
    	        	});
    	        	$("#areaId").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });
    
    //查询库位
    form.on('select(areaId)',function(){
    	$("#positionId").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/basicPosition/Querylist',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"positionAreaId":$("#areaId").val()},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>选择库位</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.id+"'>"+value.positionNumber+"</option>";
    	        	});
    	        	$("#positionId").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });

    // 工具条点击事件
    table.on('tool(' + MoveDiskDetailed.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            MoveDiskDetailed.openEditDlg(data);
        } else if (layEvent === 'delete') {
            MoveDiskDetailed.onDeleteItem(data);
        }
    });
});
